package vip.indo.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 使用HttpSessionListener统计在线用户数的监听器
 * @Author: tianlianyin
 * @Date: 2024/11/28 16:30
 * @Descripition:
 */
@Slf4j
@Component
public class MyHttpSessionListener implements HttpSessionListener {
    /**
     * 记录在线的用户个数
     */
    private Integer count=0;
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        log.info("新用户上线了");
        count++;
        httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        log.info("用户下线了");
        count--;
        httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
    }
}
